﻿using System;
using System.Collections.Generic;
using System.Linq;
using Windows.Devices.Sms;
using Windows.Foundation;
using Windows.Graphics.Display;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;

namespace RobertMcLaws.TwilioForWin8
{
    public sealed partial class GroupedCollectionPage
    {

        #region Private Members

        private IEnumerable<Object> _groups;
        private DisplayPropertiesEventHandler _displayHandler;
        private TypedEventHandler<ApplicationLayout, ApplicationLayoutChangedEventArgs> _layoutHandler;

        #endregion

        #region Properties

        public IEnumerable<Object> Groups
        {
            get { return this._groups; }
            set
            {
                this._groups = value;
                GroupedCollectionViewSource.Source = value;
            }
        }

        #endregion

        #region Page Lifecycle

        public GroupedCollectionPage()
        {
            InitializeComponent();
            BackButton.IsEnabled = false;
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            if (_displayHandler == null)
            {
                _displayHandler = Page_OrientationChanged;
                _layoutHandler = Page_LayoutChanged;
            }
            DisplayProperties.OrientationChanged += _displayHandler;
            ApplicationLayout.GetForCurrentView().LayoutChanged += _layoutHandler;
            SetCurrentViewState(this);
        }

        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            DisplayProperties.OrientationChanged -= _displayHandler;
            ApplicationLayout.GetForCurrentView().LayoutChanged -= _layoutHandler;
        }

        private void Page_LayoutChanged(object sender, ApplicationLayoutChangedEventArgs e)
        {
            SetCurrentViewState(this);
        }

        private void Page_OrientationChanged(object sender)
        {
            SetCurrentViewState(this);
        }

        private void SetCurrentViewState(Control viewStateAwareControl)
        {
            VisualStateManager.GoToState(viewStateAwareControl, this.GetViewState(), false);
        }

        private String GetViewState()
        {
            var orientation = DisplayProperties.CurrentOrientation;
            if (orientation == DisplayOrientations.Portrait ||
                orientation == DisplayOrientations.PortraitFlipped) return "Portrait";
            var layout = ApplicationLayout.Value;
            if (layout == ApplicationLayoutState.Filled) return "Fill";
            if (layout == ApplicationLayoutState.Snapped) return "Snapped";
            return "Full";
        }

        #endregion

        void Header_PointerReleased(object sender, PointerEventArgs e)
        {
            // Construct the appropriate destination page and set its context appropriately
            var group = (sender as FrameworkElement).DataContext as ICollectionViewGroup;
            var collection = group.Name as IEnumerable<Object>;
            App.ShowCollectionSummary(collection);
        }

        void ItemGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Construct the appropriate destination page and set its context appropriately
            var selectedItem = (sender as Selector).SelectedItem as Expression.Blend.SampleData.SampleDataSource.TwilioDataItem;
            App.ShowDetail(selectedItem.Collection, selectedItem);
        }

        private void SmsButton_Click(object sender, RoutedEventArgs e)
        {
            var device = SmsDevice.GetDefault();
            if (device != null && device.DeviceStatus == SmsDeviceStatus.Ready)
            {
                try 
                { 
                    // defines a text message 
                    var smsMessage = new SmsTextMessage 
                    {
                        To = "18005551212", 
                        Body = "Sent via the Windows 8 SMS SDK Sample"
                    };
                    var sendSmsMessageOperation = device.SendMessageAsync(smsMessage); 
 
                }
                catch (Exception ex)
                { 
                    //TODO: Handle exception here
                } 
            }
            else
            {
                var client = RobertMcLaws.TwilioForWin8.App.TwilioClient;
                client.SendSmsMessage("18005551212", "18005551212", "Sent via the Twilio API");
            }
        }
    }
}
